<template>
	<view>
		<categories></categories>
		<view class="content">
			<scroll-view class="menu-wrapper" scroll-y scroll-with-animation="true">
				<view class="left-content">
					<view style="position: relative;" v-for="(item,index) in goods" :key="index" ref="menuList"
						@click="select(index)" :class="{'current': currentIndex == index}">
						<view class="menu-item">{{item.name}}</view>
						<text class="allcount" v-if="getAllCount>=item.count&&item.count>0">{{item.count}}</text>
					</view>
					<navigator url="../dialog/dialog">
						<image src="../../static/icons/ymy/Button_goods.png" class="button_logo"></image>
					</navigator>
				</view>
			</scroll-view>
			<!-- 右侧滚动 -->
			<scroll-view class="foods-wrapper" scroll-y :style="'height:'+windows_height+'px'" :scroll-top="foodSTop"
				@scroll="myscroll" scroll-with-animation="true">
				<view ref="foodsWrapper">
					<view ref="foodList" class="foods" v-for="(item, i) in goods" :key="i">
						<view class="food-title">
							{{item.name}}
						</view>
						<view class="food" v-for="(food, index) in item.foods" :key="index">
							<image v-show="food.img!=undefined" :src="food.img" mode=""
								style="width: 80rpx;height: 80rpx;margin-top: 6px;"></image>
							<view class="food-info">
								<text style="font-size: 15px;margin-top: 2px;">{{food.name}}</text>
								<!-- 加减 -->
								<view class="food-btm">
									<text style="font-size: 13px;margin: 2px 0;">{{food.description}}</text>
									<cartcontrol :food="food" @add="addCart" @dec="decreaseCart"></cartcontrol>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
			<shopcart :goods="goods" @add="addCart" @dec="decreaseCart" @delAll="delAll"></shopcart>
		</view>
	</view>
</template>

<script>
	import categories from '@/components/listCategories/categories.vue';
	import shopcart from '@/components/listCategories/shopcart.vue';
	import cartcontrol from '@/components/listCategories/cartcontrol.vue'
	import Vue from 'vue'
	import {
		createNamespacedHelpers
	} from "vuex";
	let {
		mapState,
		mapActions
	} = createNamespacedHelpers("carBeauty");
	export default {
		data() {
			return {
				title: 'Hello',
				goods: [{
						"name": "常用推荐",
						"foods": [{

							"name": "普通保养",
							"price": 9.22,
							"oldPrice": "",
							"description": "美享机油5w-40L×1 欧非机滤 清器",
							"sellCount": 229

						}, {
							"name": "豪华保养",
							"price": 9.8,
							"oldPrice": "",
							"description": "美享机油5w-40L×1 欧非机滤 清器×1 BOSH空气滤清器×1 马勒燃油滤清器×1",
							"sellCount": 239

						}, ]
					},
					{
						"name": "空调护养",
						"foods": [{
							"name": "空调滤清器",
							"price": 1.88,
							"oldPrice": "",
							"description": "马勒燃油滤清器×1",
							"sellCount": 229,
							"img": '../../static/images/ymy/A14/image_ranyou2.png'

						}, {
							"name": "空调滤清器2",
							"price": 9.9,
							"oldPrice": "",
							"description": "BOSH空气滤清器×1",
							"sellCount": 239,
							"img": '../../static/images/ymy/A14/image_ranyou2.png'

						}],

					},
					{
						"name": "引擎维护",
						"foods": [{
							"name": "机油×2",
							"price": 1.88,
							"oldPrice": "",
							"description": "美享机油5w-40L×1",
							"sellCount": 229,
							"img": '../../static/images/ymy/A14/image_ranyou2.png'

						}, {
							"name": "机油×3",
							"price": 9.9,
							"oldPrice": "",
							"description": "美享机油5w-40L×1",
							"sellCount": 239,
							"img": '../../static/images/ymy/A14/image_ranyou2.png'

						}],

					},
					{
						"name": "制动系统",
						"foods": [{
							"name": "机滤2",
							"price": 1.88,
							"oldPrice": "",
							"description": "欧非机滤",
							"sellCount": 229,
							"img": '../../static/images/ymy/A14/image_ranyou2.png'

						}, {
							"name": "机滤3",
							"price": 9.9,
							"oldPrice": "",
							"description": "欧非机滤",
							"sellCount": 239,
							"img": '../../static/images/ymy/A14/image_ranyou2.png'

						}],

					},
					{
						"name": "冷却系统",
						"foods": [{
							"name": "清器1",
							"price": 9.22,
							"oldPrice": "",
							"description": "BOSH空气滤清器×1",
							"sellCount": 229,
							"img": '../../static/images/ymy/A14/image_ranyou2.png'

						}, {
							"name": "清器1",
							"price": 9.8,
							"oldPrice": "",
							"description": "BOSH空气滤清器×1",
							"sellCount": 239,
							"img": '../../static/images/ymy/A14/image_ranyou2.png'

						}]
					},
				],

				windows_height: 0, //屏幕高度
				foodSTop: 0, //右侧的滑动值
				currentIndex: 0,
				last: 0,
				right_height: [], //右侧每个内容的高度集合
				select_index: 0,
				left_height: 0, //左侧总高度
				left_scroll: 0, //左侧滑动值
				catrgoryList: [],
				timeout: null
			}

		},
		mounted(){
			this.getGoodsList()
		},
		components: {
			shopcart,
			cartcontrol,
			categories
		},
		onLoad() {
			this.windows_height = Number(uni.getSystemInfoSync().windowHeight) - 225;
			setTimeout(() => {
				this.getHeightList();
			}, 1000)
		},

		computed: {
			getList() {
				let result = [];
				this.goods.forEach((good) => {

					good.foods.forEach((food) => {

						if (food.count) {
							result.push(food)
						}
					})
				})
				return result

			},
			// 获得购物车所有商品数量
			getAllCount: function(item) {
				let result = [];
				let count = 0;

				for (let i = 0; i < this.goods.length; i++) {
					count = 0;
					this.goods[i].foods.forEach((food) => {
						if (food.count >= 0) {
							count += food.count
							Vue.set(this.goods[i], 'count', count)
						}
					})
					result.push(count)
				}

				result.sort(function(a, b) {
					return a - b;
				})
				count = result[result.length - 1]
				return count;
			},

		},
		methods: {
			...mapActions(["getGoodList"]),
			//获取所有商品
			async getGoodsList() {
				const data = await this.getGoodList();
			},
			// 点击侧边栏
			select(index) {
				this.currentIndex = index;
				this.setScrollH(index);
			},

			// 设置点击侧边栏右边滚动的高度
			setScrollH: function(index) {
				var that = this;
				let height = 0;
				var query = uni.createSelectorQuery();
				let foods = query.selectAll('.foods');

				this.$nextTick(function() {
					foods.fields({
						size: true
					}, data => {
						if (index == 0) {
							that.foodSTop = 0;
						}
						for (let i = 0; i < index; i++) {

							height += parseInt(data[i].height);
							// console.log('fh', foods);
							that.foodSTop = height;
							// console.log(that.foodSTop)
						}

					}).exec();
				})

			},

			addCart: function(item) {
				if (item.count >= 0) {
					item.count++
					this.goods.forEach((good) => {
						good.foods.forEach((food) => {
							// 根据名字添加购物车,实际环境根据需要更改
							if (item.name == food.name)
								food.count = item.count
						})
					})
				} else {
					console.log('add')
					this.goods.forEach((good) => {
						good.foods.forEach((food) => {
							if (item.name == food.name)
								Vue.set(food, 'count', 1)
						})
					})
				}
			},
			decreaseCart(item) {
				if (item.count) {
					item.count--
					this.goods.forEach((good) => {
						good.foods.forEach((food) => {
							if (item.name == food.name)
								food.count = item.count
						})
					})
				}
			},
			// 清空购物车
			delAll() {
				this.goods.forEach((good) => {
					good.foods.forEach((food) => {
						if (food.count) {
							food.count = 0
						}
					})
				})
			},
			getHeightList() {
				let _this = this;
				let selectorQuery = uni.createSelectorQuery().in(this);
				selectorQuery.select('.left-content').boundingClientRect(function(rects) {
					_this.left_height = rects.height;
				});
				selectorQuery.selectAll('.foods').boundingClientRect(function(rects) {
					_this.right_height = rects.map((item) => item.top);
				}).exec();
			},
			myscroll(e) {
				//引入节流	
				let right_content_height = e.detail.scrollHeight - this.windows_height;
				if (right_content_height == e.detail.scrollTop) {
					return;
				}
				let scroll_top = e.detail.scrollTop + 110; //110是banner图的高度
				//判断当前的scrollTop在哪个区间内;
				let now = +new Date();
				if (now - this.last > 100) {
					this.last = now;
					let active_index = this.right_height.findIndex((value, index, arr) => value <= scroll_top &&
						scroll_top < arr[index + 1]);
					this.currentIndex = active_index;
					if (this.left_height - this.windows_height) {
						//如果有超出部分
						let diff = this.left_height - this.windows_height
						this.left_scroll = Math.round((active_index * diff) / (this.goods.length - 1))
					}
				}
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		position: absolute;
		top: 340rpx;
		bottom: 55px;
		width: 100%;
		overflow: hidden;
	}

	.button_logo {
		width: 76rpx;
		height: 76rpx;
	}

	.current {
		position: relative;
		z-index: 340rpx;
		background-color: #fff;
		color: #00A0DC;
		border-left: 5px solid #00A0DC;
	}

	.menu-wrapper {
		text-align: center;
		width: 22%;
		display: flex;
		flex-direction: column;
		background: #f3f5f7;

	}

	.menu-item {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		height: 50px;
	}

	.allcount {
		position: absolute;
		right: 6px;
		top: 8px;
		display: inline-block;
		padding: 0 4px;
		font-size: 9px;
		line-height: 16px;
		font-weight: 400;
		border-radius: 50%;
		background-color: #f01414;
		color: #ffffff;
	}

	.foods-wrapper {
		width: 78%;
		height: 100%;
	}

	.foods {
		margin-bottom: 20rpx;
	}

	.food {
		display: flex;
		flex-direction: row;
		background-color: #FFFFFF;
	}

	.food-btm,
	.content {
		display: flex;
		flex-direction: row;
	}

	.food-title {
		width: 168rpx;
		height: 48rpx;
		border-radius: 0rpx 200rpx 0rpx 0rpx;
		background-color: #FFFFFF;
		padding-left: 20rpx;
		font-family: PingFang SC;
		font-size: 24rpx;
		font-weight: normal;
		font-stretch: normal;
		line-height: 48rpx;
		letter-spacing: 0rpx;
		color: #464646;
	}

	.food-info {
		margin-left: 10px;
		margin-right: 16px;
		display: flex;
		flex-direction: column;
		flex: 2;
	}

	.food-btm {
		justify-content: space-between;
	}

	.food-price {
		color: #f01414;
		font-size: 16px;
	}
</style>
